package com.sservice.base.utils.service;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * Created by suweiming on 2018/1/16.
 */
public class SService {


    public static String CURRENT_SP = "current"; // 登录用户session KEY
    public static String USER = "loginUser"; // 管理后台登录用户session KEY

    /**
     * 按规则进行评分
     *
     * @param score
     * @param thisTime
     * @return
     */
    public static String genScore(String score, String thisTime) {
        if (score == null) {
            score = "0:0:0|0:0:0|0";
        }
        BigDecimal totalNum = BigDecimal.valueOf(Double.valueOf(score.split("\\|")[2])); //总评价次数
        //String[] scores = score.split("\\|")[0].split("\\:");
        String[] totals = score.split("\\|")[1].split("\\:");
        BigDecimal[] thisScore = toBigDecimalArray(thisTime.split("\\:"), 3);
        BigDecimal[] totalScore = toBigDecimalArray(totals, 3);
        BigDecimal[] avgScore = new BigDecimal[3];
        avgScore[0] = totalScore[0].add(thisScore[0]).divide(totalNum.add(BigDecimal.ONE), RoundingMode.HALF_DOWN); //五舍六入，负数先取绝对值再五舍六入再负数
        avgScore[1] = totalScore[1].add(thisScore[1]).divide(totalNum.add(BigDecimal.ONE),RoundingMode.HALF_DOWN);
        avgScore[2] = totalScore[2].add(thisScore[2]).divide(totalNum.add(BigDecimal.ONE),RoundingMode.HALF_DOWN);
        return avgScore[0] + ":" + avgScore[1] + ":" + avgScore[2] + "|"
                + (totalScore[0].add(thisScore[0])) + ":" + (totalScore[0].add(thisScore[0])) + ":" + (totalScore[0].add(thisScore[0])) + "|" + totalNum.add(BigDecimal.ONE);
    }

    static BigDecimal[] toBigDecimalArray(String[] scores, int num) {
        BigDecimal[] avgScore = new BigDecimal[num];
        int i = 0;
        for (String s : scores) {
            BigDecimal time = BigDecimal.valueOf(Double.valueOf(s));
            avgScore[i++] = time;
        }
        return avgScore;
    }

}
